package com.bhprod.comatule.detail;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bhprod.comatule.R;
import com.bhprod.comatule.model.Photo;
import com.bhprod.comatule.util.Constants;
import com.bhprod.comatule.util.Tools;

public class SpeciesDetailFragment extends Fragment {

	private Photo photo;
	private Context context;
//	private boolean siblingMinusTwo = false;
	private boolean siblingMinusOne = false;
//	private boolean siblingPlusTwo = false;
	private boolean siblingPlusOne = false;

//	public void setSiblingMinusTwo(boolean siblingMinusTwo) {
//		this.siblingMinusTwo = siblingMinusTwo;
//	}

	public void setSiblingMinusOne(boolean siblingMinusOne) {
		this.siblingMinusOne = siblingMinusOne;
	}

//	public void setSiblingPlusTwo(boolean siblingPlusTwo) {
//		this.siblingPlusTwo = siblingPlusTwo;
//	}

	public void setSiblingPlusOne(boolean siblingPlusOne) {
		this.siblingPlusOne = siblingPlusOne;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public SpeciesDetailFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.detail_species_fragment, container, false);
		
		String idpath = Tools.completeIdWithZeros(photo.getId(), getResources().getConfiguration().locale);

		String filename = Constants.PHOTOS_PATH + idpath + Constants.PHOTOS_EXTENSION;
		
		try {
			InputStream path = context.getAssets().open(filename);
			Bitmap bit = BitmapFactory.decodeStream(path);

			ImageView image = (ImageView) rootView.findViewById(R.id.speciesDetailImage);
			image.setImageBitmap(bit);
		} catch (IOException e) {
			Log.e(SpeciesDetailFragment.class.getName(),
					"Cant't find file " + filename, e);
		}

//		if (!siblingMinusTwo) {
//			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorImageMinusTwo);
//			icon.setVisibility(View.INVISIBLE);
//		}
		if (!siblingMinusOne) {
			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorDetailImageMinusOne);
			icon.setVisibility(View.INVISIBLE);
		}
		if (!siblingPlusOne) {
			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorDetailImagePlusOne);
			icon.setVisibility(View.INVISIBLE);
		}
//		if (!siblingPlusTwo) {
//			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorImagePlusTwo);
//			icon.setVisibility(View.INVISIBLE);
//		}

		return rootView;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

}
